﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Xml;

namespace CommentHero
{
    #region Commentsクラス
    /// <summary>
    /// コメント定義クラス
    /// </summary>
    public class Comments
    {
        #region フィールド
        private HeaderComment _classHeaderComment;
        private HeaderComment _methodHeaderComment;
        private HeaderComment _propertyHeaderComment;
        private HeaderComment _constructorHeaderComment;
        private HeaderComment _testCaseHeaderComment;
        private HeaderComment _interfaceHeaderComment;
        private List<string> _blockComment; // ブロックコメント
        private List<string> _customComment1; // カスタムコメント1
        private List<string> _customComment2; // カスタムコメント2
        private List<string> _customComment3; // カスタムコメント3
        private List<string> _customComment4; // カスタムコメント4
        private List<string> _customComment5; // カスタムコメント5
        #endregion

        #region プロパティ
        /// <summary>
        /// クラスコメント
        /// </summary>
        public HeaderComment ClassHeaderComment
        {
            get
            {
                return this._classHeaderComment;
            }
        }

        /// <summary>
        /// メソッドコメント
        /// </summary>
        public HeaderComment MethodHeaderComment
        {
            get
            {
                return this._methodHeaderComment;
            }
        }

        /// <summary>
        /// プロパティコメント
        /// </summary>
        public HeaderComment PropertyHeaderComment
        {
            get
            {
                return this._propertyHeaderComment;
            }
        }

        /// <summary>
        /// コンストラクタコメント
        /// </summary
        public HeaderComment ConstructorHeaderComment
        {
            get
            {
                return this._constructorHeaderComment;
            }
        }

        /// <summary>
        /// テストケースコメント
        /// </summary>
        public HeaderComment TestCaseHeaderComment
        {
            get
            {
                return this._testCaseHeaderComment;
            }
        }

        /// <summary>
        /// インターフェースコメント
        /// </summary>
        public HeaderComment InterfaceHeaderComment
        {
            get
            {
                return this._interfaceHeaderComment;
            }
        }

        /// <summary>
        /// ブロックコメント
        /// </summary>
        public List<string> BlockComment
        {
            get
            {
                return this._blockComment;
            }
        }

        /// <summary>
        /// カスタムコメント1
        /// </summary>
        public List<string> CustomComment1
        {
            get
            {
                return this._customComment1;
            }
        }

        /// <summary>
        /// カスタムコメント2
        /// </summary>
        public List<string> CustomComment2
        {
            get
            {
                return this._customComment2;
            }
        }

        /// <summary>
        /// カスタムコメント3
        /// </summary>
        public List<string> CustomComment3
        {
            get
            {
                return this._customComment3;
            }
        }

        /// <summary>
        /// カスタムコメント4
        /// </summary>
        public List<string> CustomComment4
        {
            get
            {
                return this._customComment4;
            }
        }

        /// <summary>
        /// カスタムコメント5
        /// </summary>
        public List<string> CustomComment5
        {
            get
            {
                return this._customComment5;
            }
        }
        #endregion

        /// <summary>
        /// コンストラクタ
        /// </summary>
        /// <param name="elements">&lt;cs&gt;/&lt;vb&gt;のXML要素</param>
        public Comments(XmlElement elements)
        {
            this._classHeaderComment = new HeaderComment(
                ReadNodes(elements, "header/class/out"),
                ReadNodes(elements, "header/class/xmlsummary"),
                ReadNodes(elements, "header/class/xmlremarks"));
            this._methodHeaderComment = new HeaderComment(
                ReadNodes(elements, "header/method/out"),
                ReadNodes(elements, "header/method/xmlsummary"),
                ReadNodes(elements, "header/method/xmlremarks"));
            this._propertyHeaderComment = new HeaderComment(
                ReadNodes(elements, "header/property/out"),
                ReadNodes(elements, "header/property/xmlsummary"),
                ReadNodes(elements, "header/property/xmlremarks"));
            this._constructorHeaderComment = new HeaderComment(
                ReadNodes(elements, "header/constructor/out"),
                ReadNodes(elements, "header/constructor/xmlsummary"),
                ReadNodes(elements, "header/constructor/xmlremarks"));
            this._testCaseHeaderComment = new HeaderComment(
                ReadNodes(elements, "header/testcase/out"),
                ReadNodes(elements, "header/testcase/xmlsummary"),
                ReadNodes(elements, "header/testcase/xmlremarks"));
            this._interfaceHeaderComment = new HeaderComment(
                ReadNodes(elements, "header/interface/out"),
                ReadNodes(elements, "header/interface/xmlsummary"),
                ReadNodes(elements, "header/interface/xmlremarks"));
            this._blockComment = ReadNodes(elements, "comment/block/out");
            this._customComment1 = ReadNodes(elements, "comment/custom1/out");
            this._customComment2 = ReadNodes(elements, "comment/custom2/out");
            this._customComment3 = ReadNodes(elements, "comment/custom3/out");
            this._customComment4 = ReadNodes(elements, "comment/custom4/out");
            this._customComment5 = ReadNodes(elements, "comment/custom5/out");
        }

        /// <summary>
        /// ノード読み込み
        /// </summary>
        /// <param name="elements">&lt;cs&gt;/&lt;vb&gt;のXML要素</param>
        /// <param name="path">ノードパス</param>
        /// <returns>コメント文字列リスト</returns>
        private List<string> ReadNodes(XmlElement elements, string path)
        {
            List<string> comments = new List<string>();

            XmlNodeList nodes = elements.SelectNodes(path);

            foreach (XmlNode str in nodes)
            {
                try
                {
                    comments.Add(str.InnerText);
                }
                catch
                {
                }
            }

            if (comments.Count == 0)
            {
                // ノードが存在しなかったら空白を1つ挿入
                comments.Add(string.Empty);
            }

            return comments;
        }

        /// <summary>
        /// カスタムコメント取得
        /// </summary>
        /// <param name="index">インデックス(1～5)</param>
        /// <returns>カスタムコメント文字列リスト</returns>
        public List<string> GetCustomComment(int index)
        {
            switch (index)
            {
                case 1:
                    return this._customComment1;
                case 2:
                    return this._customComment2;
                case 3:
                    return this._customComment3;
                case 4:
                    return this._customComment4;
                case 5:
                    return this._customComment5;
                default:
                    return null;
            }
        }
    }
    #endregion
}
